#include "CGrabWidget.h"





CGrabWidget::CGrabWidget(QWidget *parent):QWidget(parent){
    isDown=false;
    setAttribute(Qt::WA_TranslucentBackground);
}

void CGrabWidget::mousePressEvent(QMouseEvent *e){
    isDown=true;
    pDown=e->pos();
}

void CGrabWidget::mouseMoveEvent(QMouseEvent *e){
    if(isDown)
    {
        pLast=e->pos();
    }
    update();
}

void CGrabWidget::mouseReleaseEvent(QMouseEvent *e){
    isDown=false;
    pLast=e->pos();
    QPoint tl(qMin(pDown.x(),pLast.x()),qMin(pLast.y(),pDown.y()));
    QPoint diff=pLast-pDown;
    emit sGrab(QRect(tl.x(),tl.y(),qAbs(diff.x()),qAbs(diff.y())));
}

void CGrabWidget::paintEvent(QPaintEvent *e){
    QPainter p(this);
    p.fillRect(this->rect(),QColor(200,200,200,95));
    if(isDown)
    {
        p.setPen(qRgb(1,0,0));
        p.drawRect(QRect(pDown,pLast));
    }
}
